package com.avic.szzx.service;

import com.alibaba.fastjson.JSONObject;
import com.avic.szzx.controller.bean.quality_tracing.EquipmentInformationCorrespondence;
import com.avic.szzx.controller.bean.quality_tracing.ProcessRecord;
import com.avic.szzx.controller.bean.quality_tracing.relationship.EquipmentProcessRelationship;
import com.avic.szzx.exception.BusinessException;
import com.avic.szzx.exception.EmBusinessError;
import com.avic.szzx.util.EsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
@Service
public class BusinessProcessingService {
    @Autowired
    private MysqlService mysqlService;

    @Autowired
    private KafkaService kafkaService;

    /**
     * 写入装备配套信息对应实例数据
     * @param status
     * @param type
     * @param tag
     * @param datas
     * @return
     */
    public boolean writeEquipmentInfoCorrespondence(Boolean status,String type, String tag,List<EquipmentInformationCorrespondence> datas) {
        if (!datas.isEmpty()) {
            for (EquipmentInformationCorrespondence data : datas) {
                data.setId(EsUtils.getId());
                int result = mysqlService.insertEquipmentInformationCorrespondenceData(data);
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag,"ADD");
                } else {
                    log.error("#############################关系型数据库插入失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }

    public boolean writeEquipmentInfoCorrespondences(Boolean status,String type, String tag,List<EquipmentInformationCorrespondence> datas) {
        List<String> strObjectMaps = new ArrayList<>();
        if (!datas.isEmpty()) {
            for (EquipmentInformationCorrespondence data : datas) {
                data.setId(EsUtils.getId());
                int result = mysqlService.insertEquipmentInformationCorrespondenceData(data);
                if (result < 0) {
                    log.error("#############################关系型数据库插入失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
                LinkedHashMap<String, Object> stringObjectMap = EsUtils.ObjToMap(data);
                stringObjectMap.put("tag", tag);
                strObjectMaps.add(JSONObject.toJSONString(stringObjectMap));
            }
            kafkaService.sendKafkaMessages(status, type, strObjectMaps, tag);
        }
        return true;
    }
    /**
     * 写入加工记录信息对应实例数据
     * @param status
     * @param type
     * @param tag
     * @param datas
     * @return
     */
    public boolean writeProcessRecord(Boolean status,String type, String tag,List<ProcessRecord> datas) {
        if (!datas.isEmpty()) {
            for (ProcessRecord data : datas) {
                data.setId(EsUtils.getId());
                int result = mysqlService.insertProcessRecordata(data);
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag,"ADD");
                } else {
                    log.error("#############################关系型数据库插入失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }
    public boolean updateEquipmentInfoCorrespondence(Boolean status,String type, String tag,List<EquipmentInformationCorrespondence> datas) {
        if (!datas.isEmpty()) {
            for (EquipmentInformationCorrespondence data : datas) {
                int result = mysqlService.updateEquipmentInformationCorrespondenceData(data);
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag,"UPDATE");
                } else {
                    log.error("#############################关系型数据库插入失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }

    /**
     * 写入装备和加工记录实例数据
     * @param status
     * @param type
     * @param tag
     * @param datas
     * @return
     */
    public boolean writeEquipmentProcessRelationship(Boolean status,String type, String tag,List<EquipmentProcessRelationship> datas) {
        if (!datas.isEmpty()) {
            for (EquipmentProcessRelationship data : datas) {
                data.setId(EsUtils.getId());
                int result = mysqlService.insertEquipmentProcessRelationshipData(data);
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag,"ADD");
                } else {
                    log.error("#############################关系型数据库插入失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }

    public boolean deleteEquipmentInfoCorrespondence(Boolean status,String type, String tag,List<EquipmentInformationCorrespondence> datas) {
        if (!datas.isEmpty()) {
            for (EquipmentInformationCorrespondence data : datas) {
                int result = mysqlService.deleteData(tag, data.getId());
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag, "DELETE");
                } else {
                    log.error("#############################关系型数据库删除失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }
    public boolean deleteProcessRecord(Boolean status,String type, String tag,List<ProcessRecord> datas) {
        if (!datas.isEmpty()) {
            for (ProcessRecord data : datas) {
                int result = mysqlService.deleteData(tag, data.getId());
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag, "DELETE");
                } else {
                    log.error("#############################关系型数据库删除失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }
    public boolean deleteEquipmentProcessRelationship(Boolean status, String type, String tag, List<EquipmentProcessRelationship> datas) {
        if (!datas.isEmpty()) {
            for (EquipmentProcessRelationship data : datas) {
                int result = mysqlService.deleteData(tag, data.getId());
                if (result > 0) {
                    kafkaService.sendKafkaMessage(status, type, data, tag, "DELETE");
                } else {
                    log.error("#############################关系型数据库删除失败,id为:[{}]", data.getId());
                    throw new BusinessException(EmBusinessError.DB_INSERT_FAILED.getErrCode(), EmBusinessError.DB_INSERT_FAILED.getErrMsg());
                }
            }
        }
        return true;
    }
}